<?php /* УТФ-8:fix */

class SPIcontroller__catalogs__front extends SPIcontroller__catalogs
{
	public function buildCatalogsTree( & $tree, $curCat = null, $pid = null, $isOnlyIdList = false )
	{	
		$list = SPIm::I('catalogs')->front->select( $pid );
		
		$isIssetActiveItem = false;
		
		if( count($list) > 0 )
		{
			foreach( $list as $item )
			{
				$item['is_active'] = $this->buildCatalogsTree( $item['items'], $curCat, $item['id'] );
				
				if( $isOnlyIdList === false )
				{
					if( $item['link'] == null )
					{
						$item['link'] = SPIc::I('front')->link->content( 'product', null, $item['id'], null, $item['name'] );
					}
					
					if($item['id'] == $curCat || $item['is_active'])
					{
						$item['is_active'] = true;
						$isIssetActiveItem = true;
					}
				}
				/*
				$rootCat = $this->getRootCatalog($item['id']);
				if( $pid === null && $rootCat['id'] == $item['id'] )
				{
					$item['is_open'] = true;
				}
				*/
				$tree[] = $isOnlyIdList ? $item['id'] : $item;
			}
		}
		
		return $isOnlyIdList ? true : $isIssetActiveItem;
	}
	
	/*
	public function getRootCatalog( $id )
	{	
		$item = SPIm::I('catalogs')->front->get_by_id($id );
		
		while( $item['pid'] !== null )
		{				
			$item = SPIm::I('catalogs')->front->get_by_id( $item['pid'] );
		}
		return $item;
	}
	*/
	
	public function get_fuul_way_catalog( $id )
	{
		$way = array();
		
		if( !is_null( $id ) ){
			$result = SPIm::I('catalogs')->front->get_by_id( $id );
			$item['link'] = SPIc::I('front')->link->content( 'product', null, $result['id'], null, $result['name'] );
			$item['name'] = $result['name'];
			$item['anons'] = $result['anons'];
			$item['description'] = $result['description'];
			$way[] = $item;
		}
		
		while( $result['pid'] !== null )
		{				
			$result = SPIm::I('catalogs')->front->get_by_id( $result['pid'] );
			$item['link'] = SPIc::I('front')->link->content(  'product', null, $result['id'], null, $result['name'] );
			$item['name'] = $result['name'];
			$item['anons'] = $result['anons'];
			$item['description'] = $result['description'];
			$way[] = $item;
		}
		$way = array_reverse($way);
		return $way;
	}
	
	public function getCatalogsWayIds( $catId )
    {
        $list = array();

        $index = 100; // max catalog level
        while( $index-- >= 0 && $catId != 0 )
        {
            $item = SPIm::I('catalogs')->front->get_by_id( $catId );

            $list[] = $item['id'];

            $catId = $item['pid'];
        }
		
        return $list;
    }
	
	/*
	public function build_list( $select_mode, $pid_catalog = null, $active_catalog_id = null )
	{
		$result = array();
		
		$orderby['field'] = 'order_index';
		$orderby['dir'] = '+';
		
		$qresult = SPIm::I('catalogs')->front->select( $select_mode, $pid_catalog, null, $orderby );
		if(count($qresult) > 0)
		foreach ($qresult as $i){
			$i['link'] = SPIc::I('front')->link->catalog_content( 'faq', $i['id'], $i['name'] );
			$i['is_active'] = $active_catalog_id !== null && $i['id'] == $active_catalog_id;

			$result[] = $i;
		}
		if ($active_catalog_id === false){
			$result[0]['is_active']=true;
		}

		return $result;
	}
	*/
	
	
	public function getCatalogData( $catId )
	{
		$catId = (int) $catId;
		
		$catalogIds = SPIc::I('catalogs')->front->getCatalogsWayIds( $catId );
		$catalogIds = array_merge( array($catId), $catalogIds );
		
		foreach( $catalogIds as $itemId )
		{
			$catalogItem = SPIm::I('catalogs')->front->get_by_id($itemId);
			$catalogItem['preview'] = SPIm::I('media')->get_public_sourceByIndexOld(
				$this->MEDIA_GID, $catalogItem['id'], 1,
				array( 'mode' => 'preview' )
			);
			$clearDescr = strip_tags( $catalogItem['description'] );
			$catalogItem['isShowFullDescription'] = empty( $clearDescr ) === false;
			
			if( trim($catalogItem['anons']) )	// если анонс пустой то не показываем (в будущем мож сделать флажок в админке)
				return $catalogItem;
		}
		
		return false;
	}
	
	
}
